﻿(function () {
    var serviceId = 'common';

    function common($rootScope) {

        function getQueryStringParameter(paramToRetrieve) {
            var params =
                document.URL.split("?")[1].split("&");
            var strParams = "";
            for (var i = 0; i < params.length; i = i + 1) {
                var singleParam = params[i].split("=");
                if (singleParam[0] === paramToRetrieve)
                    return singleParam[1];
            }
        }

        var hostweburl = decodeURIComponent(
            getQueryStringParameter("SPHostUrl")
        );
        var rootSiteUrl = decodeURIComponent(
            getQueryStringParameter("SPSiteUrl")
        );

        var appweburl = decodeURIComponent(
            getQueryStringParameter("SPAppWebUrl")
        );
        var listName = getQueryStringParameter('ListName');


        $rootScope.listName = 'Announcements';

        var service = {
            hostweburl: hostweburl,
            rootSiteUrl: rootSiteUrl,
            appweburl: appweburl,
            listName: 'Announcements'
        };

        return service;
    }

    angular.module('SpaDemo').factory(serviceId, ['$rootScope', common]);
})();